/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wikikii.revealbanner;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wikikii.bannerlib.banner.IndicatorLocation;
import com.wikikii.bannerlib.banner.LoopLayout;
import com.wikikii.bannerlib.banner.LoopStyle;
import com.wikikii.bannerlib.banner.OnDefaultImageViewLoader;
import com.wikikii.bannerlib.banner.bean.BannerInfo;
import com.wikikii.bannerlib.banner.view.BannerBgContainer;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * MyAdapter
 *
 * @param <T> 泛型
 * @since 2021-05-27
 */
public class MyAdapter<T> extends XRecyclerView.RecyclerAdapter<T> {
    private Context mContext;

    /**
     * 构造函数
     *
     * @param context   上下文
     * @param data      数据
     * @param mLayoutId 资源id
     */
    public MyAdapter(Context context, List data, int mLayoutId) {
        super(context, data, mLayoutId);
        mContext = context;
    }

    @Override
    public List getData() {
        return super.getData();
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @Override
    public T getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer parent) {
        return super.getComponent(position, convertComponent, parent);
    }

    @Override
    public void setNumColumns(int numColumns) {
        super.setNumColumns(numColumns);
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        super.setOnItemClickListener(onItemClickListener);
    }

    @Override
    public void bind(ViewHolder holder, T s, int position) {
        LoopLayout loopLayout = holder.<LoopLayout>getView(ResourceTable.Id_loop_layout);
        BannerBgContainer bannerBgContainer = holder.<BannerBgContainer>getView(ResourceTable.Id_banner_bg_container);
        setLoop(loopLayout, bannerBgContainer);
    }

    private void setLoop(LoopLayout loopLayout, BannerBgContainer bannerBgContainer) {
        loopLayout.setLoop_ms(3000); // 轮播的速度(毫秒)
        loopLayout.setLoop_duration(400); // 滑动的速率(毫秒)
        loopLayout.setScaleAnimation(true); // 设置是否需要动画
        loopLayout.setIndicatorLocation(IndicatorLocation.Center); // 指示器位置-中Center
        loopLayout.initializeData(mContext);
        // 准备数据
        ArrayList<BannerInfo> bannerInfos = new ArrayList<>();
        List<Object> bgList = new ArrayList<>();
        bannerInfos.add(new BannerInfo(ResourceTable.Media_banner_1, "first"));
        bannerInfos.add(new BannerInfo(ResourceTable.Media_banner_2, "second"));
        bgList.add(ResourceTable.Media_banner_bg1);
        bgList.add(ResourceTable.Media_banner_bg2);
        // 设置监听
        loopLayout.setOnLoadImageViewListener(new OnDefaultImageViewLoader() {
            @Override
            public void onLoadImageView(Image view, Object object) {
                Glide.with(view.getContext())
                        .load(object)
                        .into(view);
            }
        });
        loopLayout.setLoopData(bannerInfos);
        bannerBgContainer.setBannerBackBg(mContext, bgList);
        loopLayout.setBannerBgContainer(bannerBgContainer);
        loopLayout.startLoop();
    }
}
